1 ========================================================================
2 ACTIVEX CONTROL DLL : MFCActiveX Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 MFCActiveX demonstrates an ActiveX control written in Microsoft Foundation
9 Classes (MFC). ActiveX controls (formerly known as OLE controls) are small
10 program building blocks that can work in a variety of different containers,
11 ranging from software development tools to end-user productivity tools. For
12 example, it can be used to create distributed applications that work over
13 the Internet through web browsers. ActiveX controls can be written in MFC,
14 ATL, C++, C#, Borland Delphi and Visual Basic. In this sample, we focus on
15 writing an ActiveX control using MFC. We will go through the basic steps of
16 adding a main dialog, properties, methods, and events to the control.
18 MFCActiveX exposes the following items:
20 1. A MFC ActiveX control short-named MFCActiveX.
22 Program ID: MFCACTIVEX.MFCActiveXCtrl.1
23 CLSID_MFCActiveX: E389AD6C-4FB6-47AF-B03A-A5A5C6B2B820
24 DIID__DMFCActiveX: 0327DD42-7A9E-415B-B9A0-4AEEE1A3319E
25 DIID__DMFCActiveXEvents: 97B9B2F3-E95A-49D4-ACA3-E2A181424FD8
26 LIBID_MFCActiveXLib: DFFC673C-E5FE-4D0D-99CA-6FB4BDCF0A50
29 // The main dialog of the control
31 // The property page of the control
32 IDD_PROPPAGE_MFCACTIVEX
35 // With both get and set accessor methods
39 // HelloWorld returns a BSTR "HelloWorld"
40 BSTR HelloWorld(void);
41 // GetProcessThreadID outputs the running process ID and thread ID
42 void GetProcessThreadID(LONG* pdwProcessId, LONG* pdwThreadId);
45 // FloatPropertyChanging is fired before new value is set to the
46 // FloatProperty property. The Cancel parameter allows the client to cancel
47 // the change of FloatProperty.
48 void FloatPropertyChanging(FLOAT NewValue, VARIANT_BOOL* Cancel);
51 /////////////////////////////////////////////////////////////////////////////
54 MFCCOMClient -> MFCActiveX
55 MFCCOMClient demonstrates the use of the MFC ActiveX control.
57 MFCActiveX - CSActiveX - VBActiveX
58 These samples expose the same UI and the same set of properties, methods, and
59 events, but they are implemented in different languages.
62 /////////////////////////////////////////////////////////////////////////////
65 To build MFCActiveX, 1. run Visual Studio as administrator because the
66 control needs to be registered into HKCR. 2. Be sure to build the MFCActiveX
67 project using the Release configuration!
70 /////////////////////////////////////////////////////////////////////////////
73 A. Creating the project
75 Step1. Create a Visual C++ / MFC / MFC ActiveX Control project named
76 MFCActiveX in Visual Studio 2008.
78 Step2. In the page "Control Settings", select "Create control based on" as
79 STATIC. Under "Additional features", check "Activates when visible" and
80 "Flicker-free activation", and un-check "Has an About box dialog".
82 B. Adding a main dialog to the control
84 Step1. In Resource View, insert a new dialog resource and change the control
87 Step2. Change the default properties of the dialog to Border - None,
88 Style - Child, System Menu - False, Visible - True.
90 Step3. Create a class for the dialog, by right clicking on the dialog and
91 selecting Add Class. Name the class CMainDialog, with the base class CDialog.
93 Step4. Add the member variable m_MainDialog of the type CMainDialog to the
94 class CMFCActiveXCtrl.
96 Step5. Select the class CMFCActiveXCtrl in Class View. In the Properties
97 sheet, select the Messages icon. Add OnCreate for the WM_CREATE message.
99 Step6. Open MFCActiveXCtrl.cpp, and add the following code to the OnCreate
100 method to create the main dialog.
102 m_MainDialog.Create(IDD_MAINDIALOG, this);
104 Step7. Add the following code to the OnDraw method to size the main dialog
105 window and fill the background.
107 m_MainDialog.MoveWindow(rcBounds, TRUE);
108 CBrush brBackGnd(TranslateColor(AmbientBackColor()));
109 pdc->FillRect(rcBounds, &brBackGnd);
111 C. Adding Properties to the ActiveX control
113 Step1. In Class View, expand the element MFCActiveXLib. Right click on
114 _DMFCActiveX, and click on Add, Add Property. In the Add Property Wizard
115 dialog, select FLOAT for Property type, and enter "FloatProperty" for
116 property name. Select "Get/Set methods" to create the methods
117 GetFloatProperty and SetFloatProperty.
119 Step2. In the class CMFCActiveXCtrl, add a member variable,
120 FLOAT m_FloatField. In the class's contructor, set the variable's default
123 Step3. Associate the Get/Set methods of FloatProperty with m_FloatField.
125 D. Adding Methods to the ActiveX control
127 Step3. In Class View, expand the element MFCActiveXLib. Right click on
128 _DMFCActiveX, and click on Add, Add Method. In the Add Method Wizard
129 dialog, select BSTR for the return type, and enter "HelloWorld" for Method
132 With the almost same steps, the method GetProcessThreadID is added to get the
133 executing process ID and thread ID:
135 void GetProcessThreadID(LONG* pdwProcessId, LONG* pdwThreadId);
137 E. Adding Events to the ActiveX control
139 Step1. In Class View, right click on CMFCActiveXCtrl, select Add, Add Event.
140 In the Add Event Wizard, enter "FloatPropertyChanging" for Event name and add
141 two parameters: FLOAT NewValue, VARIANT_BOOL* Cancel.
143 Step2. The event "FloatPropertyChanging" is fired in SetFloatProperty:
145 void CMFCActiveXCtrl::SetFloatProperty(FLOAT newVal)
147 AFX_MANAGE_STATE(AfxGetStaticModuleState());
149 // Fire the event, FloatPropertyChanging
150 VARIANT_BOOL cancel = VARIANT_FALSE;
151 FloatPropertyChanging(newVal, &cancel);
153 if (cancel == VARIANT_FALSE)
155 m_fField = newVal; // Save the new value
158 // Display the new value in the control UI
159 CString strFloatProp;
160 strFloatProp.Format(_T("%f"), m_fField);
161 m_MainDialog.m_StaticFloatProperty.SetWindowTextW(strFloatProp);
167 /////////////////////////////////////////////////////////////////////////////
170 The ABCs of MFC ActiveX Controls
171 http://msdn.microsoft.com/en-us/library/ms968497.aspx
173 A Complete ActiveX Web Control Tutorial By David Marcionek
174 http://www.codeproject.com/KB/COM/CompleteActiveX.aspx
177 /////////////////////////////////////////////////////////////////////////////